package com.wjh.kt_vod.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.kt_vod.model.vod.Video;
import com.wjh.kt_vod.service.VideoService;
import com.wjh.kt_vod.mapper.VideoMapper;
import com.wjh.kt_vod.service.VodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 *
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video>
    implements VideoService {

    @Autowired
    private VodService vodService;


    /**
     * 根据课id删除小结
     * @param courseId
     */
    @Override
    public void removeVideoByCourseId(Long courseId) {
//        根据课程id查出所有小结
        LambdaQueryWrapper<Video> videoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        videoLambdaQueryWrapper.eq(Video::getCourseId,courseId);
        List<Video> videos = baseMapper.selectList(videoLambdaQueryWrapper);
        videos.stream().forEach(video -> {
            String videoSourceId = video.getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId)){
                vodService.removeById(videoSourceId);
            }
        });
        baseMapper.delete(videoLambdaQueryWrapper);
    }


    /**
     * 删除课程小结时 同时删除视频
     * @param id
     */
    @Override
    public void removeVideoById(Long id) {

        String videoSourceId = baseMapper.selectById(id).getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)){
            //根据视频id删除
            vodService.removeById(videoSourceId);
        }
        baseMapper.deleteById(id);
    }

    /**
     * 根据章节id 删除小结 及视频
     * @param chapterId
     */
   public void removeVideoByChapterId(Long chapterId){
       LambdaQueryWrapper<Video> videoLambdaQueryWrapper = new LambdaQueryWrapper<>();
       videoLambdaQueryWrapper.eq(Video::getChapterId,chapterId);
       List<Video> videos = baseMapper.selectList(videoLambdaQueryWrapper);
       videos.stream().forEach(video -> {
           String videoSourceId = video.getVideoSourceId();
           if (!StringUtils.isEmpty(videoSourceId)){
               vodService.removeById(videoSourceId);
           }
       });
       baseMapper.delete(videoLambdaQueryWrapper);
   }
}




